package com.ss.android.article.share.d;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.text.TextUtils;

import com.a.a.NightMode;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;
import com.ss.android.article.share.e.c;
import com.ss.android.article.share.entity.BaseShareContent;
import com.ss.android.article.share.entity.ShareAction;
import com.ss.android.common.app.AbsApplication;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.lib.MobClickCombiner;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX.Req;
import com.tencent.mm.opensdk.modelmsg.WXAppExtendObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage.IMediaObject;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;

public final class h extends c {
    public boolean a;
    public static NightMode b;
    private Context c;
    private IWXAPI d;
    private static h e;
    private static final Object f = new Object();
    private long g;
    private static Class h;
    private boolean i;
    private static int j = 500;
    private static int k = 1000;
    private int l;

    private h(Context arg3) {

        this.g = 0;
        this.i = false;
        this.a = true;
        this.c = arg3.getApplicationContext();
        if (AbsApplication.getCtx() != null && !TextUtils.equals(arg3.getPackageName(), AbsApplication.getCtx().getPackageName())) {
            this.c = AbsApplication.getCtx();
        }
    }

    public static h a(Context arg2, boolean arg3) {
        Object v1 = f;
        synchronized (v1) {
            if (e == null) {
                e = new h(arg2);
            }
        }
        e.i = arg3;
        return e;
    }

    public final void a(boolean arg2) {
        int v0 = arg2 ? 1 : 0;
        this.l = v0;
    }

    public final boolean a() {
        String v0_5;
        String v2;
        boolean v1_2;
        Throwable v1;
        boolean v0_1;
        int v0;
        int v12 = 3;
        int v13 = 2;
        long v4 = 0;
        if (this.d != null) {
            if (this.g == v4 || System.currentTimeMillis() - this.g >= 1800000) {
                v0 = 1;
            } else {
                this.g = System.currentTimeMillis();
                return true;
            }
        }
        String v6 = com.ss.android.article.share.b.b.a(ShareAction.wx);
        if (!android.support.design.a.isTextEmpty(v6)) {
            long v8 = System.currentTimeMillis();
            try {
                this.d = WXAPIFactory.createWXAPI(this.c, v6, true);
                v0_1 = this.d.registerApp(v6);
                if (v0_1) {

                } else {
                    IWXAPI v1_1 = null;
                    this.d = v1_1;
                }
            } catch (Throwable v0_2) {
                v1 = v0_2;
                v0_1 = false;
                v1.printStackTrace();
                this.d = null;
            }
            Logger.d("WXShareHelper", "init Wx: " + (System.currentTimeMillis() - v8));
            v1_2 = v0_1;
        } else {
            v1_2 = false;
        }

        boolean v9 = this.d != null ? true : false;
        if (!v9) {
            String v7 = AppLog.g(this.c);
            c();
            if (h == null) {
                v2 = "cannot get WXApiImplComm class";
            } else {
                Class v0_3 = h;
                Class[] v10 = new Class[v12];
                v10[0] = Context.class;
                v10[1] = String.class;
                v10[v13] = Boolean.TYPE;
                Object[] v11 = new Object[v12];
                v11[0] = this.c;
                v11[1] = "com.tencent.mm";
                v11[v13] = Boolean.TRUE;
                Object v0_4 = android.support.design.a.a(v0_3, "validateAppSignatureForPackage", v10, v11, new Object[0]);
                if ((v0_4 instanceof Boolean)) {
                    v0_5 = ((Boolean) v0_4).booleanValue() ? "validateAppSignature success" : "validateAppSignature failure";
                    v2 = v0_5;
                } else {
                    v2 = "validateAppSignatureForPackage doesnot return boolean type";
                }
            }

            v0_5 = v7 == null ? "failed_to_get_signature_hash" : "signature_hash " + v7;
            JSONObject v8_1 = new JSONObject();
            try {
                v8_1.put("validateAppSignatureResult", v2);
                v8_1.put("registerResult", v1_2);
                v8_1.put("signatureHash", v0_5);
                v8_1.put("wxAppId", v6);
            } catch (Exception v0_6) {
                v0_6.printStackTrace();
            }
            MobClickCombiner.onEvent(this.c, "weixin_share", "wei_share_check", v4, v4, v8_1);
        }
        return v9;
    }
    @SuppressLint("ResourceType")
    public final boolean a(BaseShareContent arg17) {
        String v3_4;
        int v2_9;
        byte[] v3_3;
        String v2_5;
        String v15;
        JSONObject v10;
        long v8;
        long v6;
        String v5;
        String v4;
        Context v3_2;
        byte[] v2_3;
        Context v2_2;
        WXMediaMessage v14;
        IMediaObject v11_1;
        WXWebpageObject v3_1;
        com.ss.android.article.share.entity.c v12;
        boolean v2;
        if (this.c == null) {
            v2 = false;
            return v2;
        }

        if (!this.a()) {
            UIUtils.showImageToast(this.c, R.drawable.close_popup_textpage, R.string.toast_weixin_not_install);
            return false;
        }

        if (arg17 != null && this.d != null) {
            try {
                v12 = new com.ss.android.article.share.entity.c(arg17);
                int v13 = this.l;
                v2 = this.i;
                if (v13 == 0) {
                    if ((v2) && !TextUtils.isEmpty(v12.a.getExtraString())) {
                        WXAppExtendObject v3 = new WXAppExtendObject();
                        v3.extInfo = v12.a.getExtraString();
                        v11_1 = v3;
                    } else {
                        v3_1 = new WXWebpageObject();
                        v3_1.webpageUrl = v12.a.getTargetUrl();
                        v11_1 = v3_1;
                    }
                } else {
                    v3_1 = new WXWebpageObject();
                    v3_1.webpageUrl = v12.a.getTargetUrl();
                    v11_1 = v3_1;
                }

                v14 = new WXMediaMessage();
                v2_2 = this.c;
                if (v12.a.getMedia() != null) {
                    v2_3 = v12.a.getMedia().getImageBytes(v2_2);
                } else {
                    v2_3 = null;
                }

                if (v2_3 != null) {
                    v14.thumbData = v2_3;
                }else {
                    try {
                        v3_2 = this.c;
                        v4 = "ImageUtils";
                        v5 = "getCachedImageBytes";
                        v6 = 0;
                        v8 = 0;
                        v10 = new JSONObject();
                        v15 = "image_url";
                        String v2_6;
                        if (v12.a.getMedia() != null) {
                            v2_6 = v12.a.getMedia().getUrl();
                        } else {
                            v2_6 = null;
                        }
                        MobClickCombiner.onEvent(v3_2, v4, v5, v6, v8, v10.put(v15, v2_6));
                    } catch (JSONException v2_4) {

                    }

                    v2_2 = this.c;
                    if (v2_2 == null) {

                    } else {
                        try {
                            if (!(v2_2.getResources().getDrawable(com.ss.android.article.browser.R.drawable.ly) instanceof BitmapDrawable)) {
                            } else {
                                InputStream v2_8 = v2_2.getResources().openRawResource(com.ss.android.article.browser.R.drawable.ly);
                                v3_3 = new byte[16384];
                                v2_9 = v2_8.read(v3_3, 0, 16384);
                                if (v2_9 < 16384) {
                                    byte[] v4_1 = new byte[v2_9];
                                    System.arraycopy(v3_3, 0, v4_1, 0, v2_9);
                                    v14.thumbData = v4_1;
                                }
                            }
                        } catch (Exception v2_7) {
                            v2_7.printStackTrace();
                        }
                    }
                }

                v2_5 = android.support.design.a.c(v12.a.getTitle(), j);
                v4 = android.support.design.a.c(v12.a.getText(), k);
                v14.mediaObject = (v11_1);
                if (TextUtils.isEmpty(((CharSequence) v2_5))) {
                    v2_5 = this.c.getString(com.ss.android.article.browser.R.string.app_name);
                }

                if (TextUtils.isEmpty(((CharSequence) v4))) {
                    v4 = this.c.getString(com.ss.android.article.browser.R.string.app_name);
                }

                if (this.a) {
                    v3_4 = v2_5;
                } else {
                    v3_4 = v4;
                }
                if (v13 != 0) {
                    v2_5 = v3_4;
                }
                v14.title = v2_5;
                v14.description = v4;
                Req v2_10 = new Req();
                v2_10.transaction = String.valueOf(System.currentTimeMillis());
                v2_10.scene = this.l;
                v2_10.message = v14;
                this.d.sendReq(v2_10);
                v2 = true;
            } catch (Exception v2_1) {
                Logger.w("WXShareHelper", "get message execption" + v2_1.toString());
                v2 = false;
            }

            return v2;
        }

        Logger.w("WXShareHelper", "share weixin init parameters is null");
        return false;
    }

    public static void b() {
    }

    private static void c() {
        Class v1 = h.class;
        synchronized (v1) {
            if (h == null) {
                try {
                    h = Class.forName("com.tencent.mm.sdk.openapi.WXApiImplComm");
                } catch (Exception e) {

                }
            } else {
                return;
            }
        }
    }
}

